package com.byjh.api;


import cn.hutool.core.date.DateUtil;
import com.byjh.app.domain.Chapter;
import com.byjh.app.domain.Question;
import com.byjh.app.service.*;
import com.byjh.common.annotation.Anonymous;
import com.byjh.common.core.domain.R;
import com.byjh.common.utils.SecurityUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/index")
public class IndexApi {


    @Resource
    private IAppService appService;
    @Resource
    private IBannerService bannerService;

    @Resource
    private IChapterService chapterService;

    @Resource
    private IQuestionService questionService;

    @Resource
    private IErrorCollectSetService errorSetService;


    @GetMapping("/data")
    @Anonymous
    public R<Map<String, Object>> index() {
        Long userId = SecurityUtils.getLoginUserId();
        Map<String, Object> map = new HashMap<>();
        // 获取应用列表
        map.put("app", appService.selectAppList(null));
        // 获取banner列表
        map.put("banner", bannerService.selectBannerList(null));
        // 获取科目列表
        if (userId == null) {
            map.put("subject", chapterService.selectChapterList(null));
        } else {
            // 获取用户刷题进度
            map.put("subject", chapterService.selectChapterListAndProgressByUserId(userId));
        }
        return R.ok(map);
    }


    @GetMapping("/subjectList")
    @Anonymous
    public R<List<Chapter>> subjectList() {
        Long userId = SecurityUtils.getLoginUserId();
        List<Chapter> subjectList = new ArrayList<>();
        // 获取科目列表
        if (userId == null) {
            subjectList = chapterService.selectChapterList(null);
        } else {
            subjectList = chapterService.selectChapterListAndProgressByUserId(userId);
        }
        return R.ok(subjectList);
    }


    /**
     * 我的错题 / 收藏集
     * 1 错题集 2 收藏集
     *
     * @return
     */
    @GetMapping("/mineErrorCollectSet/{type}")
    public R<Map<String, Object>> mineErrorCollectSet(@PathVariable("type") Integer type) {
        Map<String, Object> map = new HashMap<>();
        Long userId = SecurityUtils.getUserId();
        // 获取今日错题数
        map.put("toDayCount", errorSetService.selectErrorSetCount(userId, type, DateUtil.today()));
        // 获取全部错题数
        map.put("count", errorSetService.selectErrorSetCount(SecurityUtils.getUserId(), type, null));
        // 获取科目列表
        map.put("list", errorSetService.selectErrorSetList(userId, type));
        return R.ok(map);
    }

    @Anonymous
    @GetMapping("/search")
    public R<Map<String, Object>> search(@RequestParam("keyword") String keyword) {
        Map<String, Object> map = new HashMap<>();
        // 获取科目列表（10个）
        map.put("subjectList", chapterService.selectChapterList(new Chapter().setName(keyword)));
        // 获取题目列表（10个）
        map.put("questionList", questionService.selectQuestionList(new Question().setContent(keyword).setLimit(10)));
        return R.ok(map);
    }


}
